package com.jfinal.weixin.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.weixin.model.*;
import com.jfinal.weixin.sdk.api.*;
import com.jfinal.weixin.sdk.utils.StringUtil;
import com.jfinal.weixin.template.DataItem;
import com.jfinal.weixin.template.TempItem;
import com.jfinal.weixin.template.TempToJson;

import java.util.List;

/**
 * Created by 446705260@qq.com on 16/5/16.
 * chengzhichao
 */
public class TestController extends Controller {

    public static DataItem getdata() {
        DataItem dataItem = new DataItem();
        dataItem.setFirst(new TempItem("红安天气每日播报,略微偏凉，还是蛮舒适的。", "#FF0000"));
        dataItem.setKeyword1(new TempItem("17°-23° 多云", "#8CEA00"));
        dataItem.setKeyword2(new TempItem("17° 小雨", "#8CEA00"));
        dataItem.setKeyword3(new TempItem("弱", "#8CEA00"));
        dataItem.setKeyword4(new TempItem("西南风2级,湿度100%", "#8CEA00"));
        dataItem.setKeyword5(new TempItem("白天温度适中，但早晚凉，墨迹天气建议您准备一件外套来应对早晚的温差，对于年老体弱者，保暖、防风的便携外套是首选。", "#8CEA00"));
        dataItem.setRemark(new TempItem("❤❤❤❤❤❤❤❤❤❤❤❤❤\n取消订阅或者恢复订阅请回复《修改订阅状态》,签到数量已重置,请在6月3日前联系happycoolgg恢复签到次数。\n❤❤❤❤❤❤❤❤❤❤❤❤❤", "#FF0000"));
        return dataItem;
    }

    public static int sendone(String openid) {
        ApiConfigKit.setThreadLocalApiConfig(new ApiConfig(WeixinInfo.dao.getWeixinInfoById(1)));
        if (!openid.equals("oyHNpvyBm9wx26RJ4Sbj4zAzekT4")) {
            return 0;
        }
        String templateid = "EQ2mO8iVT-AfX3A6Mx64_OgPK-dta0Ic81XppoWQBu8";
        DataItem dataItem = getdata();
        List<User> list = User.dao.find("select openid,nickname from user where subscribe=1 and dingyue = 0");
        for (int i = 0; i < list.size(); i++) {
            openid = list.get(i).toString();
             String jsonStr = TempToJson.getTempJson(list.get(i).getStr("openid"), templateid,
                "#743A3A", StringUtil.HBSM, dataItem);
            ApiResult result = TemplateMsgApi.send(jsonStr);
            System.out.println(list.get(i).get("nickname", "未知用户") + result.getJson() + i);
        }
        return 1;
    }

    public static int send446705260(String openid) {
        if (!openid.equals("oyHNpvyBm9wx26RJ4Sbj4zAzekT4")) {
            return 0;
        }
        RedPackApiController redPackApiController = new RedPackApiController();
        ApiConfigKit.setThreadLocalApiConfig(new ApiConfig(WeixinInfo.dao.getWeixinInfoById(1)));
        //List<User> list = User.dao.find("select * from user where user.openid NOT  IN  ( SELECT openid from redpack_log) and subscribe=1");
        List<User> list = User.dao.find("select * from user where subscribe=1 and score >=10");
        StringBuffer stringBuffer = new StringBuffer("");
        for (int i = 0; i < list.size(); i++) {
            String result1 = redPackApiController.send(list.get(i).get("openid").toString(), "红安人专属", "红安人专属");
            stringBuffer.append(result1);
        }
        return 1;
    }

    public static int bill2db(String openid) {
        if (!openid.equals("oyHNpvyBm9wx26RJ4Sbj4zAzekT4")) {
            return 0;
        }
        RedPackApiController redPackApiController = new RedPackApiController();
        ApiConfigKit.setThreadLocalApiConfig(new ApiConfig(WeixinInfo.dao.getWeixinInfoById(1)));
        List<RedPackLog> redPackLogList = RedPackLog.dao.find("select * from redpack_log");

        for (int i = 0; i < redPackLogList.size(); i++) {
            RedPackLog redPackLog = redPackLogList.get(i);
            redPackLog.set("openid", redPackApiController.query(redPackLog.get("mch_billno").toString()).get("openid").toString());
            redPackLog.set("result_code", redPackApiController.query(redPackLog.get("mch_billno").toString()).get("result_code").toString());
            redPackLog.update();
        }
        return 1;
    }

    public static int sendto(String openid) {
        if (!openid.equals("oyHNpvyBm9wx26RJ4Sbj4zAzekT4")) {
            return 0;
        }
        ApiConfigKit.setThreadLocalApiConfig(new ApiConfig(WeixinInfo.dao.getWeixinInfoById(1)));
        //String openid = "oyHNpv3ChXLAVWw0VpZ0RAGZ7WRc";
        openid = "oyHNpvyBm9wx26RJ4Sbj4zAzekT4";//程智超
        String templateid = "EQ2mO8iVT-AfX3A6Mx64_OgPK-dta0Ic81XppoWQBu8";
        DataItem dataItem = getdata();
        String apiResult = UserApi.getFollows().getJson();
        JSONObject jobj = JSONObject.parseObject(apiResult);
        apiResult = jobj.get("data").toString();
        jobj = JSONObject.parseObject(apiResult);
        apiResult = jobj.get("openid").toString();
        JSONArray data = JSONArray.parseArray(apiResult);
        String jsonStr = TempToJson.getTempJson(openid, templateid,
                "#743A3A", StringUtil.HBSM, dataItem);
        ApiResult result = TemplateMsgApi.send(jsonStr);
        System.out.println(result.getJson());
        return 1;
    }


    public void kf(){
        ApiConfigKit.setThreadLocalApiConfig(new ApiConfig(WeixinInfo.dao.getWeixinInfoById(1)));
       CustomServiceApi.sendText(StringUtil.CZC,"this is a test");
    }

    public void updatembvote(){
        List<MbUser> mbUserList=  MbUser.dao.find("select * from mbuser");
        MbUser mbUser;
        for(int i=0;i<mbUserList.size();i++){
            mbUser=mbUserList.get(i);
            int id = mbUser.getInt("id");
            System.out.println(id+"已经更新");
            mbUser.set("curscore", MbVote.dao.getScoreOfSomeOne(mbUser.getInt("id")));
            mbUser.update();
        }

//        int id = 153;
//        MbUser mbUser =MbUser.dao.findById(id);
//        mbUser.set("curscore",MbVote.dao.getScoreOfSomeOne(id));
//        mbUser.update();
    }

}
